﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Text;


public partial class TraSuperise_Next_List : System.Web.UI.Page
{
    public string _strProjectId;
    private string _strAccountId, UserCode;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
             HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
             if (_Account != null)
             {
                 _strProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
                 UserCode = _Account.UserCode;
                 _strAccountId = _Account.AccountId.ToString();
               
                 if (!IsPostBack)
                 {
                     BindArea();
                 }

                 showGridView();
             }
        }
    }


    /// <summary>
    /// 绑定地区列表
    /// </summary>
    private void BindArea()
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string strSql = "select * from area  where areaid >530000 and areaid<540000 and areaid%100=0";
        Db DbProject = new Db();

        System.Data.IDataReader dr = DbProject.DataReader(strSql);
        lstAreaName.Items.Clear();
        while (dr.Read())
        {
            string areaname = dr["areaname"].ToString();
            string areaid = dr["areaid"].ToString();
            lstAreaName.Items.Add(new ListItem(areaname,areaid));
        }
        if (lstAreaName.Items.Count > 0)
        {
            lstAreaName.Items.Add(new ListItem("--全部--", "0"));
            lstAreaName.SelectedIndex = lstAreaName.Items.Count - 1;
        }

        dr.Close();
        dr.Dispose();
        DbProject.Close();
    }


    /// <summary>
    /// 绑定打印数据
    /// </summary>
    private void getPrintSource()
    {
        string Sql = "select GoodsId as 商品流水号,productname as 通用名,goodsname as 商品名,medicinemodel as 剂型,outlookc as 规格,factor as 转换系数," +
                "CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS 质量层次," +
                "UserCode_TB as 中标人注册号,CompanyName_TB as 中标人名称,CompanyName_PS as 主配送企业,areaname as 地区," +
                "case when SupplyPlan_GoodsNext.PS_ChangeStatus = '0' or SupplyPlan_GoodsNext.PS_ChangeStatus is null then '未审核'   when SupplyPlan_GoodsNext.PS_ChangeStatus ='1' then '审核通过' when SupplyPlan_GoodsNext.PS_ChangeStatus ='2' then '审核未通过' end as 审核状态" +
                //"SupplyPlan_GoodsNext.Accountname_shr as 审核人 " +
                " from SupplyPlan_Goods " +
                "INNER JOIN SupplyPlan_GoodsNext ON SupplyPlan_Goods.SupplyGoodsId = SupplyPlan_GoodsNext.SupplyGoodsId " +
                 " AND SupplyPlan_Goods.AccountId_PS = SupplyPlan_GoodsNext.Accountid_ps_M" +
                 " where " + fctConditions();
        Db dbfps = new Db();
        DataTable dt = dbfps.DataTable(Sql);
        this.gvList.DataSource = dt;
        gvList.DataBind();
        dbfps.Close();
    }


    /// <summary>
    /// 保存为excel文件
    /// </summary>
    private void ImportExcel()
    {
        getPrintSource();
        if (gvList.Rows.Count > 0)
        {
            gvList.Visible = true;
            Response.Clear();
            Response.ContentType = "application/x-excel";
            Response.AddHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("分配送详情" + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".xls\"");
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            StringBuilder sb = new StringBuilder();
            System.IO.StringWriter stringWrite = new System.IO.StringWriter(sb);
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            sb.Append(@"<style> .text { mso-number-format:\@; } </style> ");
            sb.Append("<html><body>");
            gvList.RenderControl(htmlWrite);
            sb.Append("</body></html>");
            Response.Write(stringWrite.ToString());
            Response.End();
            gvList.DataSource = null;
            gvList.Dispose();
        }
        else
        {
            Response.Write("<script>alert('没有信息,无法打印!')</script>");
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    } 

    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods INNER JOIN SupplyPlan_GoodsNext ON SupplyPlan_Goods.SupplyGoodsId = SupplyPlan_GoodsNext.SupplyGoodsId AND SupplyPlan_Goods.AccountId_PS = SupplyPlan_GoodsNext.Accountid_ps_M";                                               //要查询的表明
        dbPage.Key = "id";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "id,GoodsId,ProductName,MedicineModel,Outlookc,Factor,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,UserCode_TB,CompanyName_TB,CompanyName_SC,UserCode_SC,CompanyName_PS,areaname,companyname_ps_B1,case when SupplyPlan_GoodsNext.PS_ChangeStatus = '0' or SupplyPlan_GoodsNext.PS_ChangeStatus is null then '未审核'   when SupplyPlan_GoodsNext.PS_ChangeStatus ='1' then '审核通过' when SupplyPlan_GoodsNext.PS_ChangeStatus ='2' then '审核未通过' end as PS_ChangeStatus,SupplyPlan_GoodsNext.Accountname_shr,SupplyPlan_GoodsNext.NopssReason ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName_TB,productname,medicinemodel";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' nowrap>流水号</td>");
        strHtml.Append("<td class='table_title' nowrap>通用名</td>");
        strHtml.Append("<td class='table_title' nowrap>剂型</td>");
        strHtml.Append("<td class='table_title' nowrap>规格</td>");
        strHtml.Append("<td class='table_title' nowrap>转换系数</td>");
        strHtml.Append("<td class='table_title' nowrap>质量层次</td>");
        strHtml.Append("<td class='table_title' nowrap>中标人注册号</td>");
        strHtml.Append("<td class='table_title' nowrap>中标人名称</td>");
        strHtml.Append("<td class='table_title' nowrap>生产企业名称</td>");
        strHtml.Append("<td class='table_title' nowrap>生产企业注册号</td>");
        strHtml.Append("<td class='table_title' nowrap>主配送企业</td>");
        strHtml.Append("<td class='table_title' nowrap>地区</td>");
        //strHtml.Append("<td class='table_title' nowrap>分配送企业</td>");
        strHtml.Append("<td class='table_title' nowrap>审核状态</td>");
        //strHtml.Append("<td class='table_title' nowrap>审核人</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();

        while (dr.Read())
        {
            string _strCheckStatus = dr["PS_ChangeStatus"].ToString();
            if (_strCheckStatus == "2")
            {
                strHtml.Append("<tr style='background-color:Yellow'>");
            }
            else
            {
                if (i == 0)
                {
                    strHtml.Append("<tr class='table_td'>");
                    i++;
                }
                else
                {
                    strHtml.Append("<tr class='table_td2'>");
                    i--;
                }
            }

            strHtml.Append("<td>" + dr["goodsid"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["MedicineModel"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Outlookc"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Factor"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["QualityLevels"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_SC"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_SC"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_PS"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["areaname"].ToString() + "</td>");
            //strHtml.Append("<td>" + dr["companyname_ps_B1"].ToString() + "</td>");
            if (dr["PS_ChangeStatus"].ToString() == "审核未通过")
            {
                strHtml.Append("<td title='" + dr["NopssReason"].ToString() + "' style=' color: #ff0000;' >" + dr["PS_ChangeStatus"].ToString() + "</td>");
            }
            else
            {
                strHtml.Append("<td >" + dr["PS_ChangeStatus"].ToString() + "</td>");
            }
            //strHtml.Append("<td>" + dr["Accountname_shr"].ToString() + "</td>");//审核人
            //if (dr["CompanyName_PS"].ToString().Trim() == "")
            //{
            //    strHtml.Append("<td>企业未维护</td>");
            //}
            //else
            //{
            //    strHtml.Append("<td><a target=\"_blank\" href=\"TraCheckIsbidcompany_PSFinfo.aspx?id='" + dr["id"].ToString() + "'\" onClick=\"window.open(this.href,'+''''+''''+','+''''+'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=1'+ ''''+');return false;\">审核</a></td>");
            //}
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        //BId = Request.Form["bidid"].ToString();
        //GridView1.DataSource = dr;
        //GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp, CheckStatus, _ps, _GoodsId, name, outlookc, _companyname_tb, _usecoder_tb;

        name = ProductName.Text.ToString();
        outlookc = Outlookc.Text.ToString();

        _companyname_tb = Textcompanyname_tb.Text.Trim();
        _usecoder_tb = txtCompanyUserCode.Text.Trim();
        strtemp = " projectid=" + _strProjectId + " and SupplyPlan_Goods.IsUse='1' and SupplyPlan_GoodsNext.accountid_ps_b1=" + _strAccountId + "  and";

        _ps = SysFun.ToTrim(txtps.Text);
        _GoodsId = SysFun.ToTrim(txtGoodsId.Text);

        //审核状态

        CheckStatus = lstCheckStatus.SelectedValue;
        switch (CheckStatus)
        {
            case "3":  //全部
                break;
            case "0"://未审核
                strtemp += " (SupplyPlan_GoodsNext.PS_ChangeStatus=0 or SupplyPlan_GoodsNext.PS_ChangeStatus is null) and";
                break;
            case "1"://审核通过
                strtemp += " SupplyPlan_GoodsNext.PS_ChangeStatus=1 and";
                break;
            case "2"://审核未通过
                strtemp += " SupplyPlan_GoodsNext.PS_ChangeStatus=2 and";
                break;
        }


        //地区查询
        string areanameselect = lstAreaName.SelectedValue;
        if (areanameselect != "0")
        {
            strtemp += " areaid = '" + areanameselect + "' and";
        }

        if (_ps != "") //按主配送企业查询
        {
            strtemp += " CompanyName_PS like '%" + _ps + "%' and";
        }


        if (_GoodsId != "")
        {
            strtemp += " GoodsId like '%" + _GoodsId + "%' and";

        }


        if (name != "")
        {
            strtemp += " (ProductName like '%" + name + "%'or Product_SpellingCode like '%" + name + "%') and";
        }
        if (outlookc != "")
        {
            strtemp += " outlookc like '%" + outlookc + "%' and";
        }


        if (_companyname_tb!="")
        {
            strtemp += "  SupplyPlan_Goods.companyname_tb like '%" + _companyname_tb + "%'  and";
        }

        if (_usecoder_tb!="")
        {
            strtemp += " SupplyPlan_Goods.usercode_tb like '%" + _usecoder_tb + "%' and";
        }


        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }



        return strtemp;

    }


    protected void but1_ServerClick(object sender, EventArgs e)
    {
        ImportExcel();
    }
}
